package com.lcf.framework.handler;

import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface;
import cn.dev33.satoken.annotation.handler.SaCheckPermissionHandler;
import cn.dev33.satoken.stp.StpUtil;
import com.lcf.framework.annotation.PermissionCheck;
import com.lcf.framework.enums.PermissionEnum;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 自定义权限检查处理器
 *
 * @author lcf
 */
@Component
public class PermissionCheckHandler implements SaAnnotationHandlerInterface<PermissionCheck> {
    @Override
    public Class<PermissionCheck> getHandlerAnnotationClass() {
        return PermissionCheck.class;
    }

    @Override
    public void checkMethod(PermissionCheck permission, Method method) {
        String[] permissionCodes = Arrays.stream(permission.value()).map(PermissionEnum::name).toArray(String[]::new);
        SaCheckPermissionHandler._checkMethod(StpUtil.TYPE, permissionCodes, permission.mode(), permission.orRole());
    }
}
